package com.dmatek.uwb.xml.util.iassign;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.dmatek.uwb.xml.dom.annotations.AttributeName;
/***
 * 属性分配赋值工具抽象类
 * @author zhangfu
 * @data 2019年6月14日 上午10:06:06
 * @Description
 */
public abstract class BaseAssignTool implements IAssignAttributeTool {
	protected Field getField(String name, Class<?> clazz) throws SecurityException {
		while (null != clazz) {
			Field[] fields = null;
			fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				if(isMatchField(field, name)) {
					return field;
				}
			}
			clazz = clazz.getSuperclass();
		}
		return null;
	}
	protected List<Field> getAllFields(Class<?> clazz) {
		List<Field> fields = new ArrayList<Field>(30);
		while(null != clazz) {
			fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
			clazz = clazz.getSuperclass();
		}
		return fields;
	}
	private boolean isMatchField(Field field, String name) {
		AttributeName attrName = field.getAnnotation(AttributeName.class);
		if(null != attrName && attrName.value().equalsIgnoreCase(name)) {
			return true;
		}
		return false;
	}
	protected boolean isField(String name, Class<?> clazz) {
		for (Field field : getAllFields(clazz)) {
			if(isMatchField(field, name)) {
				return true;
			}
		}
		return false;
	}
}
